﻿/// <reference path="BaseValidator.js" />

var $ValidationCompareOperator = TA.Turbo.Controls.ValidationCompareOperator =
{
	EqualText: 0,
	Equal: 11,
	GreaterThan: 13,
	GreaterThanEqual: 14,
	Between: 17,
	BetweenOrEqual: 18,
	Contains: 21,
	IsContained: 23,
	DataTypeCheck: 31
};
var $ValidationValueUsage = TA.Turbo.Controls.ValidationValueUsage =
{
	FirstValue: 0,
	SecondValue: 1,
	FirstControl: 2,
	SecondControl: 3
};


TA.Turbo.Controls.CompareValidator = function CompareValidator(clientId)
{
	this.BaseValidator(clientId);

}
TA.Turbo.Controls.CompareValidator.Inherit(TA.Turbo.Controls.BaseValidator);
var ____a;
if (____a)
{
	TA.Turbo.Controls.CompareValidator.prototype = new TA.Turbo.Controls.BaseValidator();
	TA.Turbo.Controls.CompareValidator.prototype.BaseValidator = new TA.Turbo.Controls.BaseValidator();
}
var $CompareValidator = TA.Turbo.Controls.CompareValidator;

$PropertyDefinition.Define($CompareValidator, "Operator", "O", $ValidationCompareOperator.EqualText);
$PropertyDefinition.Define($CompareValidator, "Negated", "N", false);
$PropertyDefinition.Define($CompareValidator, "Type", "Ty", DataType.String);
$PropertyDefinition.Define($CompareValidator, "ComparisonValue1", "CV1", null);
$PropertyDefinition.Define($CompareValidator, "ComparisonValue2", "CV2", null);
$PropertyDefinition.Define($CompareValidator, "ComparisonControl1Id", "CC1", null);
$PropertyDefinition.Define($CompareValidator, "ComparisonControl2Id", "CC2", null);
$PropertyDefinition.Define($CompareValidator, "MainValueUsage", "MV", $ValidationValueUsage.FirstValue);
$PropertyDefinition.Define($CompareValidator, "SecondValueUsage", "SV", $ValidationValueUsage.SecondValue);

$CompareValidator.prototype.get_ComparisonControl1 = function get_ComparisonControl1()
{
	return $(this.get_ComparisonControl1Id());
}
$CompareValidator.prototype.get_ComparisonControl2 = function get_ComparisonControl2()
{
	return $(this.get_ComparisonControl2Id());
}

$CompareValidator.prototype._OnValidate = function _OnValidate()
{
	if ((this.get_Operator() == $ValidationCompareOperator.Contains || this.get_Operator() == $ValidationCompareOperator.DoesNotContain
		|| this.get_Operator() == $ValidationCompareOperator.IsContained || this.get_Operator() == $ValidationCompareOperator.IsNotContained)
		&& this.get_Type() != DataType.String)
		Throw("Unable to apply 'contain' operators to data types other than string.");

	this.set_IsValid(false);

	var value1, value2 = null, value;
	try
	{
		switch (this.get_Operator())
		{
			case $ValidationCompareOperator.Equal:
				value1 = this.ConvertToOurType(this.GetComparisonValue(this.get_MainValueUsage()));
				value = this.ConvertToOurType(this.get_ValidatedValue());
				this.set_IsValid((value == value1 ^ this.get_Negated()) == 1);
				break;
			case $ValidationCompareOperator.EqualText:
				value1 = Coalesce(this.GetComparisonValue(this.get_MainValueUsage()), "").toString();
				value = Coalesce(this.get_ValidatedValue(), "").toString();
				this.set_IsValid((value == value1 ^ this.get_Negated()) == 1);
				break;
			case $ValidationCompareOperator.GreaterThan:
			case $ValidationCompareOperator.GreaterThanEqual:
			case $ValidationCompareOperator.Between:
			case $ValidationCompareOperator.BetweenOrEqual:
				value1 = this.ConvertToOurType(this.GetComparisonValue(this.get_MainValueUsage()));
				value = this.ConvertToOurType(this.get_ValidatedValue());
				if (this.get_Operator() == $ValidationCompareOperator.Between || this.get_Operator() == $ValidationCompareOperator.BetweenOrEqual)
					value2 = this.ConvertToOurType(this.GetComparisonValue(this.get_SecondValueUsage()));

				switch (this.get_Operator())
				{
					case $ValidationCompareOperator.GreaterThan:
						if (this.get_Negated())
							this.set_IsValid(value < value1);
						else
							this.set_IsValid(value > value1);
						break;
					case $ValidationCompareOperator.GreaterThanEqual:
						if (this.get_Negated())
							this.set_IsValid(value <= value1);
						else
							this.set_IsValid(value >= value1);
						break;

					case $ValidationCompareOperator.Between:
						if (this.get_Negated())
							this.set_IsValid(value < value1 || value > value2);
						else
							this.set_IsValid(value > value1 && value < value2);
						break;
					case $ValidationCompareOperator.BetweenOrEqual:
						if (this.get_Negated())
							this.set_IsValid(value <= value1 || value >= value2);
						else
							this.set_IsValid(value >= value1 && value <= value2);
						break;
				}

				break;
			case $ValidationCompareOperator.Contains:
			case $ValidationCompareOperator.IsContained:
				var sVal = Coalesce(this.get_ValidatedValue(), "").toString()
					, sVal1 = Coalesce(this.GetComparisonValue(this.get_MainValueUsage()), "").toString();

				switch (this.get_Operator())
				{
					case $ValidationCompareOperator.Contains:
						this.set_IsValid((sVal.IndexOf(sVal1, StringComparison.CurrentCultureIgnoreCase) != -1 ^ this.get_Negated()) == 1);
						break;
					case $ValidationCompareOperator.IsContained:
						this.set_IsValid((sVal1.IndexOf(sVal, StringComparison.CurrentCultureIgnoreCase) != -1 ^ this.get_Negated()) == 1);
						break;
				}
				break;
			case $ValidationCompareOperator.DataTypeCheck:
				try
				{
					value = this.ConvertToOurType(this.get_ValidatedValue());
					this.set_IsValid(!this.get_Negated());
				}
				catch (ex)
				{
					this.set_IsValid(this.get_Negated());
				}
				break;
		}
	}
	catch (ex)
	{
		this.set_IsValid(false);
	}

}

$CompareValidator.prototype.ConvertToOurType = function ConvertToOurType(v)
{
	var s = Coalesce(v, "").toString();
	switch (this.get_Type())
	{
		case DataType.String:
			return s;
		case DataType.Integer:
			v = parseInt(s);
			if (isNaN(v))
				Throw("Failed to convert string to integer.");
			return v;
		case DataType.Double:
			v = parseFloat(s);
			if (isNaN(v))
				Throw("Failed to convert string to double.");
			return v;
		case DataType.Date:
			v = new Date(s);
			//			if (isNaN(v))
			//				Throw("Failed to convert string to integer.");
			return v;
		case DataType.Currency:
			v = parseFloat(RemoveCurrencySymbols(RemoveGroupingSymbols(s)));
			if (isNaN(v))
				Throw("Failed to convert string to decimal.");
			return v;
	}
	Throw("Unknown DataType.");
}
$CompareValidator.prototype.RemoveCurrencySymbols = function RemoveCurrencySymbols(p)
{
	return p.Replace("$", "");
}
$CompareValidator.prototype.RemoveGroupingSymbols = function RemoveGroupingSymbols(p)
{
	return p.Replace(",", "");
}
$CompareValidator.prototype.GetComparisonValue = function GetComparisonValue(valueUsage)
{
	switch (valueUsage)
	{
		case $ValidationValueUsage.FirstValue:
			return this.get_ComparisonValue1();
		case $ValidationValueUsage.SecondValue:
			return this.get_ComparisonValue2();
		case $ValidationValueUsage.FirstControl:
			return this.get_ComparisonControl1() == null ? null : this.get_ComparisonControl1().get_Object().get_Value();
		case $ValidationValueUsage.SecondControl:
			return this.get_ComparisonControl2() == null ? null : this.get_ComparisonControl2().get_Object().get_Value();
	}
	Throw("Unknown $ValidationValueUsage.");
}
